<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Finance\ReceivableRefund;
use AuctionCore\System\AuctionException;

/**
 * 应收退款
 * @author Xmh
 */
class ReceivableRefundController extends BaseController
{
    private $ReceivableRefund;
    public function __construct()
    {
        parent::__construct();
        $this->emp_id = $this->user_info['emp_id'];
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
        $this->ReceivableRefund = new ReceivableRefund();
    }
    public function index()
    {
        $branch_id = $this->user_info['branch_id'];
        //分公司列表
        $bc_list = $this->get_branch_company_list();
        $this->assign('bc_list', $bc_list);
        $this->assign('branch_id',$branch_id);


        $channel = new \AuctionCore\Other\Channel();
        $pfrom = $channel->get_parent_channel_list();
        $comefrom_list = $this->get_comefrom_list();
        //城市对应的4s店
        $city_fours_list = $this->get_fours_by_city();
        $this->assign("pfrom",$pfrom);
        $this->assign('comefrom_list',$comefrom_list);
        $this->assign('city_fours_list',$city_fours_list);
        $this->display();
    }

    /**
     * 获取应收退款列表
     */
    public function receivable_list(){
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['status'] = I('get.status');
        $params['location_area'] = I('get.location_area'); //所在地
        $params['comefrom'] = I('get.comefrom');
        $params['keywords'] = I('get.keywords');

        //所在地查询
        if(count($this->bc_city) > 0) {
            $bc_code = array_flip($this->bc_city);
            $bc_code = implode(',', $bc_code);
            $params['bc_city'] = $bc_code;
        }

        $result = $this->ReceivableRefund->get_receivable_refund_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
//        dump($list);
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('params',$params);
        $this->display();
    }

    //处理
    public function receivable_deal(){
        $refund_id = I('get.id/d',0);
        $receivable_refund_detail = $this->ReceivableRefund->receivable_refund_detail($refund_id);
        $receivable_status = $receivable_refund_detail['receivable_status'];
        if($receivable_status > 1 || empty($receivable_status)){
            $this->redirect('ReceivableRefund/index','',1,'请求出错了');
        }

        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($receivable_refund_detail['car_id']);
        $this->assign('car_detail', $car_detail);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);


        $this->assign('detail',$receivable_refund_detail);
        $this->display();
    }
    //确认收退款操作
    public function do_receivable_deal(){
        $receivable_id = I('post.receivable_id/d',0);
        $receivable_type = I('post.receivable_type/d',0);
        $receivable_amount = I('post.receivable_amount');
        if(empty($receivable_amount)){
            $this->helpJsonResult(400,'应收款金额不正确');
        }
        $receivable_remark = I('post.receivable_remark');
        $data['id'] = $receivable_id;
        $data['emp_id'] = $this->emp_id;
        $data['receivable_type'] = $receivable_type;
        $data['receivable_amount'] = $receivable_amount;
        $data['remark'] = $receivable_remark;
        $data['status'] = 2;
        $res = $this->ReceivableRefund->save_receivable_refund($data);
        if(!$res){
            $this->helpJsonResult(400, '处理失败');
        }
        /**
         * 记录车辆跟踪日志
         */
        $receivable_refund_detail = $this->ReceivableRefund->get_receivable_refund($receivable_id,'car_id,order_id');
        $car_action_data = array(
            'action_name' => '已收退款',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($receivable_refund_detail['car_id'], $car_action_data);
        /**
         * 记录拍品跟踪日志
         */
        $order_action_data = array(
            'action_name' => '已收退款',
            'emp_id' => $this->emp_id
        );
        \AuctionCore\AuctionOrder\AuOrder::add_order_trace_log($receivable_refund_detail['order_id'], $order_action_data);

        $this->helpJsonResult(200, '处理成功');
    }
    //查看
    public function receivable_view(){
        $receivable_id = I('get.id/d',0);
        $receivable_refund_detail = $this->ReceivableRefund->receivable_refund_detail($receivable_id);
        $receivable_status = $receivable_refund_detail['receivable_status'];
        if($receivable_status < 2 || empty($receivable_status)){
            $this->redirect('ReceivableRefund/index','',1,'请求出错了');
        }

        $car_detail = \AuctionCore\CarResource\Car::getDetailWithChannel($receivable_refund_detail['car_id']);
        $this->assign('car_detail', $car_detail);

        $show_from = \AuctionCore\Other\Channel::get_from();  //获取渠道来源信息
        $this->assign('show_from', $show_from);

        $transfer_type = C('TRANSFER_LIMIT');
        $this->assign('transfer_type', $transfer_type);


        $this->assign('detail',$receivable_refund_detail);
        $this->display();
    }
}
